<template>
  <div>
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form @keyup.enter.native="reload" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col :lg="8">
            <a-form-item label="订单号">
              <a-input placeholder="请输入订单号" v-model:value="queryParam.orderNo" />
            </a-form-item>
          </a-col>
          <a-col :lg="8">
            <a-form-item label="保单号">
              <a-input placeholder="请输入保单号" v-model:value="queryParam.policyNo" />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :lg="8">
              <a-form-item label="下单时间">
                <a-date-picker
                  value-format="YYYY-MM-DD"
                  placeholder="请选择开始时间"
                  v-model:value="queryParam.policyInsurDateBegin"
                  class="query-group-cust"
                />
                <span class="query-group-split-cust">~</span>
                <a-date-picker
                  value-format="YYYY-MM-DD"
                  placeholder="请选择结束时间"
                  v-model:value="queryParam.policyInsurDateEnd"
                  class="query-group-cust"
                />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="出保时间">
                <a-date-picker
                  value-format="YYYY-MM-DD"
                  placeholder="请选择开始时间"
                  v-model:value="queryParam.insureActiveDateBegin"
                  class="query-group-cust"
                />
                <span class="query-group-split-cust">~</span>
                <a-date-picker
                  value-format="YYYY-MM-DD"
                  placeholder="请选择结束时间"
                  v-model:value="queryParam.insureActiveDateEnd"
                  class="query-group-cust"
                />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="区域">
                <j-select-dept placeholder="请选择区域" :multiple="true" v-model:value="queryParam.areaId" checkStrictly />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="保险种类">
                <j-dict-select-tag placeholder="请选择保险种类" v-model:value="queryParam.insuranceType" dictCode="BXZL" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="项目名称">
                <a-input placeholder="请输入项目名称" v-model:value="queryParam.projectName" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="项目编号">
                <a-input placeholder="请输入项目编号" v-model:value="queryParam.projectId" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="被保人名称">
                <a-input placeholder="请输入被保人名称" v-model:value="queryParam.insuredName" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="投保人名称">
                <a-input placeholder="请输入投保人名称" v-model:value="queryParam.policyHolderName" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="投保联系人">
                <a-input placeholder="请输入投保联系人" v-model:value="queryParam.policyLinkmanName" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="投保联系人电话">
                <a-input placeholder="请输入投保联系人电话" v-model:value="queryParam.policyLinkmanMobilename" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="保费">
                <a-input placeholder="请输入保费" v-model:value="queryParam.premium" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="保险状态">
                <j-dict-select-tag placeholder="请选择保险状态" v-model:value="queryParam.policyState" dictCode="BXZT" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="保险公司">
                <j-dict-select-tag placeholder="请选择保险公司" v-model:value="queryParam.insuranceCompany" dictCode="BXGS" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="付款状态">
                <j-dict-select-tag placeholder="请选择付款状态" v-model:value="queryParam.payState" dictCode="ZFZT" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="付款人名称">
                <a-input placeholder="请选付款人名称" v-model:value="queryParam.recAccountName" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="发票状态">
                <j-dict-select-tag placeholder="请选择发票状态" v-model:value="queryParam.invoiceState" dictCode="FPZT" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="发票申请时间">
                <a-date-picker
                  value-format="YYYY-MM-DD"
                  placeholder="请选择开始时间"
                  v-model:value="queryParam.invoiceApplyDateBegin"
                  class="query-group-cust"
                />
                <span class="query-group-split-cust">~</span>
                <a-date-picker
                  value-format="YYYY-MM-DD"
                  placeholder="请选择结束时间"
                  v-model:value="queryParam.invoiceApplyDateEnd"
                  class="query-group-cust"
                />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="发票类型">
                <j-dict-select-tag placeholder="请选择发票类型" v-model:value="queryParam.invoiceType" dictCode="FPLX" />
              </a-form-item>
            </a-col>
            <a-col :lg="8">
              <a-form-item label="邀请码">
                <a-input placeholder="请输入邀请码" v-model:value="queryParam.invitationCodeQuery" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :lg="6">
                <a-button type="primary" preIcon="ant-design:search-outlined" @click="reload">查询</a-button>
                <a-button preIcon="ant-design:reload-outlined" @click="searchReset" style="margin-left: 8px">重置</a-button>
                <a @click="toggleSearchStatus = !toggleSearchStatus" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <Icon :icon="toggleSearchStatus ? 'ant-design:up-outlined' : 'ant-design:down-outlined'" />
                </a>
              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" v-if="isDevelopment" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="exportXls"> 导出</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction  :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{ text }">
        {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <InsuranceOrderModal @register="registerModal" @success="handleSuccess" />
    <InsuranceOrderInfoModal @register="registerInfoModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="insurance-insuranceOrder" setup>
  import { usePermission } from '/@/hooks/web/usePermission';
  import { ref, reactive, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useModal } from '/@/components/Modal';
  import InsuranceOrderModal from './components/InsuranceOrderModal.vue';
  import InsuranceOrderInfoModal from './components/InsuranceOrderModalInfo.vue';
  import { columns, searchFormSchema } from './InsuranceOrder.data';
  import { list, deleteOne,qrtk,qrfk, addBank,confirmInvoice, batchDelete, getImportUrl, getExportUrl } from './InsuranceOrder.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSelectDept from '/@/components/Form/src/jeecg/components/JSelectDept.vue';
  import JInput from '/@/components/Form/src/jeecg/components/JInput.vue';
  import {getAppEnvConfig, isDevMode} from '/@/utils/env';
  import { router } from '/@/router';
  import {filterObj} from "/@/utils/common/compUtils";
  import {useMethods} from "/@/hooks/system/useMethods";
  const { handleExportXls, handleImportXls } = useMethods();

  const checkedKeys = ref<Array<string | number>>([]);
  //注册model
  const { hasPermission } = usePermission();
  const isDevelopment = isDevMode();
  // const isDevelopment = false;
  const [registerModal, { openModal }] = useModal();
  const [registerInfoModal, { openModal: infoOpenModalll }] = useModal();
  const queryParam = ref<any>({});
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '业务订单',
      api: list,
      columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      canResize: false,
      useSearchForm: false,
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam.value);
      },
    },
    exportConfig: {
      name: '业务订单',
      url: getExportUrl,
      params: queryParam.value,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  function exportXls() {
    let paramsForm = queryParam.value;
    return handleExportXls("业务订单", getExportUrl, filterObj(paramsForm));
  }
  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    infoOpenModalll(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
  /**
   * 确认支付
   */
  async function handleConfimPay(record) {
    await qrfk({ id: record.id }, handleSuccess);
  }

  /**
   * 确认支付
   */
  async function handleAddBank(record) {
    await addBank({ id: record.id }, handleSuccess);
  }
  /**
   * 确认开票
   */
  async function  handleConfirmInvoice(record) {
    await confirmInvoice({ id: record.id }, handleSuccess);
  }


  /**
   * 确认退款
   */
  async function handleConfimRund(record) {
    await qrtk({ id: record.id }, handleSuccess);
  }
  /**
   * 确认付款
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  function openPage(arg) {
    const url = arg.url;
    const record = arg.record;
    let par = 'outOrderNo=' + record.outOrderNo + '&sign=' + record.sign;
    window.open(url + par);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */

  function getTableAction(record) {

    let con = [];
    if( record.invoiceState === 'KPZ'){
      con.push({
        label: '确认开发票',
        popConfirm: {
          title: '是否确确认开发票',
          confirm: handleConfirmInvoice.bind(null, record),
        },
        divider:true,
        ifShow: () => hasPermission('insurance:confirmInvoice')
      });
    }
    if (record.policyState === 'YTB'&&record.payState === 'YZF') {
      con.push({
        label: '确认退款',
        popConfirm: {
          title: '是否确认退款',
          confirm: handleConfimRund.bind(null, record),
        },
        ifShow: () => hasPermission('insurance:QRTK')
      });
    }
    if(isDevelopment){
      con.push({
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      });
      con.push({
        label: '模拟银行数据',
        onClick: handleAddBank.bind(null, record),
      });
    }

    con.push({
      label: '详情',
      onClick: handleDetail.bind(null, record),
    });
    return con;
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    const url = getAppEnvConfig().VITE_GLOB_DOMAIN_URL;
    if(isDevelopment){
      return [
        {
          label: '删除',
          popConfirm: {
            title: '是否确认删除',
            confirm: handleDelete.bind(null, record),
          },
        },
        {
          label: '投保信息填写',
          // onClick: openPage.bind(null, { url: 'http://localhost:8080/jeecg-boot/insurance/user/order/applyView?', record: record }),
          onClick: openPage.bind(null, { url: url + '/insurance/user/order/applyView?', record: record }),
        },
        {
          label: '支付保单页面',
          // onClick: openPage.bind(null, { url: 'http://localhost:8080/jeecg-boot/insurance/user/order/payView?', record: record }),
          onClick: openPage.bind(null, { url: url + '/insurance/user/order/payView?', record: record }),
        },
        {
          label: '保单详细页面',
          // onClick: openPage.bind(null, { url: 'http://localhost:8080/jeecg-boot/insurance/user/order/infoView?', record: record }),
          onClick: openPage.bind(null, { url: url + '/insurance/user/order/infoView?', record: record }),
        },
        {
          label: '保单开发票页面',
          // onClick: openPage.bind(null, { url: 'http://localhost:8080/jeecg-boot/insurance/user/order/invoiceView?', record: record }),
          onClick: openPage.bind(null, { url: url + '/insurance/user/order/invoiceView?', record: record }),
        },
        {
          label: '保单退保页面',
          // onClick: openPage.bind(null, { url: 'http://localhost:8080/jeecg-boot/insurance/user/order/refundView?', record: record }),
          onClick: openPage.bind(null, { url: url + '/insurance/user/order/refundView?', record: record }),
        },
      ];
    }
  }

  /* ----------------------以下为原生查询需要添加的-------------------------- */

  initRoute();
  function initRoute() {
    Object.assign(queryParam.value,router.currentRoute.value.query);
  };



  const toggleSearchStatus = ref<boolean>(false);
  const labelCol = reactive({
    xs: { span: 24 },
    sm: { span: 7 },
  });
  const wrapperCol = reactive({
    xs: { span: 24 },
    sm: { span: 16 },
  });
  /**
   * 重置
   */
  function searchReset() {
    queryParam.value = {};
    selectedRowKeys.value = [];
    //刷新数据
    reload();
  }
</script>
<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust {
      width: calc(50% - 15px);
      min-width: 100px !important;
    }
    .query-group-split-cust {
      width: 30px;
      display: inline-block;
      text-align: center;
    }
  }
</style>
